/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = wtc_tcb_route.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2015/08/27 by songbb                               */
/*******************************************************************/

#include "conntrack.h"

#include "../wtc_core.h"
#include "../wtc_comm.h"
#include "../wtc_mar.h"
#include "wtc_tcb_route.h"

int wtc_tcb_route_tmpostrecv(Conntrack *p_st_conn
                            , Apm_packet *p_analyze_in
                            , L7session_wtc *p_st_sess
                            , L7_wtc_packet *p_st_wtc_packet
                            , int i_recv_size
                            , int i_hint_index)
{
    char arr_scratch[150] = {'\0'};
    unsigned int ui_ro_version = 0;
    unsigned int ui_ro_port = 0;
    unsigned int ui_ro_aomhandle = 0;
    unsigned int ui_ro_svc_tmid_ind = 0;
    unsigned int ui_ro_ntmids = 0;
    unsigned int ui_ro_flags = 0;
    char *p_host = NULL;
    unsigned int ui_host_len = 0;
    unsigned int *p_svctmid = NULL;
    unsigned int ui_lcv;
    unsigned int ui_cliversion = 0;
    unsigned int ui_timestamp = 0;
    unsigned int ui_mchid = 0;
    unsigned int ui_slot = 0;
    unsigned int ui_clipid = 0;
    unsigned int ui_cliqaddr = 0;
    unsigned int ui_clicntxt = 0;
    unsigned int ui_clisslport = 0;
    unsigned int ui_clisslsupports = 0;
    unsigned int ui_clisslrequires = 0;

    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_ro_version, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_ro_port, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_ro_aomhandle, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_ro_svc_tmid_ind, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_ro_ntmids, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_ro_flags, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    if (wtc_mar_decode_string(p_st_sess, arr_scratch, 150, p_st_wtc_packet, &p_host, &ui_host_len) != FR_SUCC)
        goto error;
    if (ui_ro_ntmids > 0)
    {
        if ((p_svctmid = (unsigned int *) frmalloc(ui_ro_ntmids * sizeof(unsigned int))) == NULL)
        {
            WTC_MALLOCK_ERR;
            goto error;
        }
        for (ui_lcv = 0; ui_lcv < ui_ro_ntmids; ui_lcv++)
        {
            if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &p_svctmid[ui_lcv], BYTE_BIG_ENDIAN) != FR_SUCC)
                goto error;
        }
    }
    if (ui_ro_version > 1)
    {
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_cliversion, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_timestamp, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_mchid, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_slot, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_clipid, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_cliqaddr, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_clicntxt, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_clisslport, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_clisslsupports, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_clisslrequires, BYTE_BIG_ENDIAN) != FR_SUCC)
            goto error;
        if (bytebuff_skip(p_st_wtc_packet->p_st_packet, 4) != FR_SUCC)
            goto error;
    }

    wtc_free((void **) &p_host);
    wtc_free((void **) &p_svctmid);
    return FR_SUCC;
error:
    wtc_free((void **) &p_host);
    wtc_free((void **) &p_svctmid);
    return FR_FAIL;
}

int wtc_tcb_route_tmpostrecv_fad(char *p_payload
                                , unsigned int ui_payload_len
                                , unsigned int *p_ui_offset
                                , int *p_i_direction
                                , int i_recv_size
                                , int i_hint_index)
{
    unsigned int ui_ro_version = 0;
    unsigned int ui_ro_ntmids = 0;

    if (wtc_get_int(p_payload, ui_payload_len, p_ui_offset, &ui_ro_version) != FR_SUCC)
        goto error;
    if (wtc_skip(p_payload, ui_payload_len, p_ui_offset, 12) != FR_SUCC)
        goto error;
    if (wtc_get_int(p_payload, ui_payload_len, p_ui_offset, &ui_ro_ntmids) != FR_SUCC)
        goto error;
    if (wtc_skip(p_payload, ui_payload_len, p_ui_offset, 4) != FR_SUCC)
        goto error;
    if (wtc_skip_string(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
        goto error;
    if (ui_ro_ntmids > 0)
    {
        if (wtc_skip(p_payload, ui_payload_len, p_ui_offset, ui_ro_ntmids * 4) != FR_SUCC)
            goto error;
    }
    if (ui_ro_version > 1)
    {
        if (wtc_skip(p_payload, ui_payload_len, p_ui_offset, 44) != FR_SUCC)
            goto error;
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}
